<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="refresh" content="5">
        <title>Affichage</title>
    </head>
<jsp:useBean id = "gM" class = "Messages.GestionMessages"  scope = "application"/>
<%  Cookie[] cookieUtilisateur = request.getCookies();
    int cookieNbMessages = -1;
    int nbMessages = gM.getNbMessages();
    Cookie newCookie;
    
    for (int i=0; i<cookieUtilisateur.length;i++)
    {
        if(cookieUtilisateur[i].getName().equals("nbMessages"))
        {
            cookieNbMessages = Integer.parseInt(cookieUtilisateur[i].getValue());
        }
    }
    
    if(cookieNbMessages == -1)
    {
        cookieNbMessages = 0;
        newCookie = new Cookie("nbMessages", String.valueOf(nbMessages));
        response.addCookie(newCookie);
    }
    
    if(nbMessages != cookieNbMessages)
    {
        newCookie = new Cookie("nbMessages", String.valueOf(nbMessages));
        response.addCookie(newCookie);
        ArrayList<String> listeMessages = gM.getMessages(); 
        Iterator<String> itr = listeMessages.iterator();
         while (itr.hasNext()) 
         {
            String messageAfficher = itr.next();
            out.println(messageAfficher+"<br />");
         }
     }
%>
</html>
